package com.gzcc.controller;

import com.gzcc.model.Brand;
import com.gzcc.model.Category;
import com.gzcc.model.Product;
import com.gzcc.repository.BrandRepository;
import com.gzcc.repository.CartRepository;
import com.gzcc.repository.CategoryRepository;
import com.gzcc.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private BrandRepository brandRepository;
    int ishot = 0;
    int istui = 0;
    int products = 2;


    /**
    * 描述: Create by Sidake
     * MenuController
     * 函数shopList
     * 作用：将数据库中的热点商品和推荐商品信息传到前端
     * 通过降序条件查询分别将热点跟推荐商品存到hotProducts和tuiProducts中并传到前端
     * 返回前台主界面index.btl
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping()
    public ModelAndView shopList() {
        ModelAndView mv = new ModelAndView();
        List<Product> hotProducts = productRepository.findByIshotOrderByIdDesc(ishot);
        List<Product> tuiProducts = productRepository.findByIstuiOrderByIdDesc(istui);
        List<Category> categoryList = categoryRepository.findAll();
        List<List<Product>> cateAndProductList = new ArrayList<>();
        for (int i = 0; i < products; i++) {
            List<Product> productList = productRepository.findByCateidOrderById(categoryList.get(i).getId());
            cateAndProductList.add(productList);
        }
        mv.addObject("categoryList", categoryList);
        mv.addObject("cateAndProductList", cateAndProductList);
        mv.addObject("hotproducts", hotProducts);
        mv.addObject("tuiproducts", tuiProducts);
        mv.setViewName("front-end/index.btl");
        return mv;
    }

   /**
   * 描述: Create by Sidake
    * MenuController
    * 函数getDetail
    * 作用：在数据库查询相对应的商品信息并传到前端
    * 通过前端获取的商品id(itemId)查询到相对应的商品信息并将数据传到前端
    * 返回前台详细界面view.btl
   * @Author: Mr.Zhong
   * @Date: 2018/6/14
   */
    @RequestMapping(value = "/views", method = RequestMethod.GET)
    public ModelAndView getDetail(@RequestParam("Id") long Id) {
        ModelAndView mv = new ModelAndView();
        Product product = productRepository.findById((int) Id).get();
        String[] colors = product.getColor().split(",");
        mv.addObject("product", product);
        mv.addObject("colors", colors);
        mv.setViewName("front-end/views.btl");
        return mv;
    }

    /**
    * 描述: 跳转分类页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/classification")
    public ModelAndView classification() {
        List<Brand> brandList = brandRepository.findAll();
        List<Category> categoryList = categoryRepository.findAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject(brandList);
        mv.addObject(categoryList);
        mv.setViewName("front-end/category.btl");
        return mv;
    }

    /**
    * 描述: 热销商品页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/hotProducts")
    public ModelAndView selectByIshot() {
        List<Product> hotProducts = productRepository.findByIshotOrderByIdDesc(ishot);
        ModelAndView mv = new ModelAndView();
        mv.addObject("products", hotProducts);
        mv.setViewName("/front-end/category_list.btl");
        return mv;
    }

    /**
    * 描述: 推销商品页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/tuiProducts")
    public ModelAndView selectByIstui() {
        List<Product> tuiproducts = productRepository.findByIstuiOrderByIdDesc(ishot);
        ModelAndView mv = new ModelAndView();
        mv.addObject("products", tuiproducts);
        mv.setViewName("/front-end/category_list.btl");
        return mv;
    }

    /**
     * 描述:  商品页面
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping("/moreProductsByCategory")
    public ModelAndView moreProductsByCategory(@RequestParam("cateId") int cateId) {
        List<Product> moreProducts = productRepository.findByCateidOrderById(cateId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("products", moreProducts);
        mv.setViewName("/front-end/category_list.btl");
        return mv;
    }
    /**
     * 描述: 商品品牌页面
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping("/moreProductsByBrand")
    public ModelAndView moreProductsByBrand(@RequestParam("brandId") int brandId) {
        List<Product> moreProducts = productRepository.findByBrandidOrderById(brandId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("products", moreProducts);
        mv.setViewName("/front-end/category_list.btl");
        return mv;
    }
}
